// GPSwSmartPhoneView.cpp : implementation of the CGPSwSmartPhoneView class
//

#include "stdafx.h"
#include "GPSwSmartPhone.h"

#include "GPSwSmartPhoneDoc.h"
#include "GPSwSmartPhoneView.h"
#include "IPConfigDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define READ_WAIT_DELAY 1000

// CGPSwSmartPhoneView

IMPLEMENT_DYNCREATE(CGPSwSmartPhoneView, CView)

BEGIN_MESSAGE_MAP(CGPSwSmartPhoneView, CView)

	ON_BN_CLICKED(ID_STARTBUTTON, onStartButtonClick)
	ON_BN_CLICKED(ID_STOPBUTTON, onStopButtonClick)
	ON_COMMAND(ID_MENU_PICKGPSDEVICES, onMenuPickGPSDevices)

	/*ON_MESSAGE(WM_GPS_ADDED, onGPSAdded)
	ON_MESSAGE(WM_GPS_REMOVED, onGPSRemoved)*/

	ON_COMMAND(ID_MENU_SETSERVERADDRESS, &CGPSwSmartPhoneView::OnMenuSetserveraddress)
END_MESSAGE_MAP()

// CGPSwSmartPhoneView construction/destruction

CGPSwSmartPhoneView::CGPSwSmartPhoneView()
{
	// TODO: add construction code here
	//bob = new GPScomm();

}

CGPSwSmartPhoneView::~CGPSwSmartPhoneView()
{
}

BOOL CGPSwSmartPhoneView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}


// CGPSwSmartPhoneView drawing
void CGPSwSmartPhoneView::OnDraw(CDC* /*pDC*/)
{
	CGPSwSmartPhoneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	//More like to NOT do! See what I did there?
	//This causes program crashes if you put
	//buttons here at least.  Use onInitialUpdate() instead
}



// CGPSwSmartPhoneView diagnostics

#ifdef _DEBUG
void CGPSwSmartPhoneView::AssertValid() const
{
	CView::AssertValid();
}

CGPSwSmartPhoneDoc* CGPSwSmartPhoneView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGPSwSmartPhoneDoc)));
	return (CGPSwSmartPhoneDoc*)m_pDocument;
}
#endif //_DEBUG

afx_msg void CGPSwSmartPhoneView::onStartButtonClick()
{
	dataCollectThread = AfxBeginThread( dataCollectionThreadController, bob);
}

afx_msg void CGPSwSmartPhoneView::onStopButtonClick()
{
	if(bob != NULL)
	bob->stopCollecting();
	else
	{
		MessageBox(L"No GPS connections have been set.  Go to Options->Pick GPS Devices.", 
			L"Error", MB_ICONASTERISK);
	}
}

afx_msg void CGPSwSmartPhoneView::onMenuPickGPSDevices()
{
	DeviceChooserDialog *chooser = new DeviceChooserDialog();
	chooser->SetCommsObject(bob);
	chooser->Create(IDD_DEVICE_CHOOSER, NULL);
	chooser->ShowWindow(SW_NORMAL);
}

void CGPSwSmartPhoneView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	// Button Widgets
	startButton.Create(L"Start", 
		BS_PUSHLIKE | WS_VISIBLE | WS_CHILD, CRect(10,160,75,175), this, ID_STARTBUTTON);
	stopButton.Create(L"Stop",
		BS_PUSHLIKE | WS_VISIBLE | WS_CHILD, CRect(232,160,307,175), this, ID_STOPBUTTON);
	GPSCaption.Create(L"GPS connected:",
		SS_SIMPLE | WS_CHILD | WS_VISIBLE, CRect(76,160,221,175), this, ID_GPSCAPTION);
	GPSCount.Create(L"0",
		SS_BLACKFRAME | WS_CHILD | WS_VISIBLE, CRect(222,160,306,175), this, ID_GPSCOUNT);
}

afx_msg LRESULT CGPSwSmartPhoneView::onGPSAdded(WPARAM w, LPARAM l)
{
	GPSCount.Create(L"0",
		SS_BLACKFRAME | WS_CHILD | WS_VISIBLE, CRect(222,160,306,175), this, ID_GPSCOUNT);
	return NULL;
}

UINT dataCollectionThreadController( LPVOID pParam )
{
	GPScomm * gps;
	if(pParam != NULL)
		gps = (GPScomm *)pParam;
	{
		MessageBox(NULL, L"No GPS connections have been set.  Go to Options->Pick GPS Devices.", 
			L"Error", MB_ICONASTERISK);
		return 0;
	}

	MessageBox(NULL, L"Polling Loop begins.", L"Now... IT BEGINS", 0);

	while(!(gps->collectionStopped()))
		{
			gps->read_binary_mode_string();
			//gps->parse_received_buffers();
			gps->prepare_server_packet();
			if(gps->server_connection)
			{
				gps->server_connection = gps->send_to_server();
				// if return is false, error occurred trying to send to server
			}

			// do NOT join this if statement as an else to the if statement above
			if(gps->server_connection == false)
			{
				gps->store_read_buffers();
			}
			Sleep(READ_WAIT_DELAY);
		}
	MessageBox(NULL, L"Collection of data has been stopped", L"", MB_ICONEXCLAMATION);
	return 0;
}

// CGPSwSmartPhoneView message handlers

void CGPSwSmartPhoneView::OnMenuSetserveraddress()
{
	IPConfigDialog *chooser = new IPConfigDialog();
	chooser->setCommsObject(bob);
	chooser->Create(IDD_IP_CONFIG_DIALOG, NULL);
	chooser->ShowWindow(SW_NORMAL);
}
